<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="评议员">
        <el-select
          v-model="queryParams['bankSenator.reviewerName']"
          placeholder="请选择评议员"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="item in senatorList"
            :key="item.reviewerId"
            :label="item.reviewerName"
            :value="item.reviewerName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="村名字" prop="reviewVillageName">
        <el-select
          v-model="queryParams.reviewVillageName"
          placeholder="请选择村名字"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="item in villageList"
            :key="item.villageId"
            :label="item.villageName"
            :value="item.villageName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="主信贷人姓名" prop="reviewName">
        <el-input
          v-model="queryParams.reviewName"
          placeholder="请输入主信贷人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="是否了解信贷人情况" prop="reviewKnow1">
        <el-select v-model="queryParams.reviewKnow1" placeholder="请选择是否了解信贷人情况" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bank:review:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bank:review:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bank:review:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bank:review:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getReviewList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" v-show="isQuerySet" :data="reviewList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="评议编号" align="center" prop="reviewId" />
      <el-table-column label="村名字" align="center" prop="reviewVillageName" />
      <el-table-column label="户籍号" align="center" prop="reviewDomicileNumber" />
      <el-table-column label="主信贷人姓名" align="center" prop="reviewName" />
      <!--<el-table-column label="身份证号" align="center" prop="reviewNumber" />-->
      <el-table-column label="评议类型" align="center" prop="reviewType" />
      <el-table-column label="评议员" align="center" prop="bankSenator.reviewerName" />
      <el-table-column label="评议监督员" align="center" prop="reviewSupervisor" />
      <el-table-column label="评议时间" align="center" prop="reviewTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reviewTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否了解信贷人情况" align="center" prop="reviewKnow1">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.reviewKnow1"/>
        </template>
      </el-table-column>
      <el-table-column label="是否存在以下情况" align="center" prop="reviewKnow2">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bank_know2" :value="scope.row.reviewKnow2"/>
        </template>
      </el-table-column>
      <el-table-column label="农村房产情况" align="center" prop="reviewKnow3">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bank_know3" :value="scope.row.reviewKnow3"/>
        </template>
      </el-table-column>
      <el-table-column label="城区有无房产" align="center" prop="reviewKnow4">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bank_know4" :value="scope.row.reviewKnow4"/>
        </template>
      </el-table-column>
      <el-table-column label="有无车辆" align="center" prop="reviewKnow5">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bank_know5" :value="scope.row.reviewKnow5"/>
        </template>
      </el-table-column>
      <el-table-column label="年收入" align="center" prop="reviewKnow6">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bank_know6" :value="scope.row.reviewKnow6"/>
        </template>
      </el-table-column>
      <el-table-column label="工作类型" align="center" prop="reviewKnow7">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bank_know7" :value="scope.row.reviewKnow7"/>
        </template>
      </el-table-column>
      <el-table-column label="主营项目" align="center" prop="reviewKnow8" />

      <el-table-column label="备注" align="center" prop="reviewNotes" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bank:review:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bank:review:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 在表格上方显示提示信息 -->
    <el-alert
      v-show="!isQuerySet"
      type="info"
      title="提示：请先选择评议员！"
      :closable="false"
    ></el-alert>
    <pagination
      v-show="total>0 "
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getReviewList"
    />

    <!-- 添加或修改评议界面对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="村名字" prop="reviewVillageName">
          <el-input v-model="form.reviewVillageName" placeholder="请输入村名字" />
        </el-form-item>
        <el-form-item label="评议户籍号" prop="reviewDomicileNumber">
          <el-input v-model="form.reviewDomicileNumber" placeholder="请输入评议户籍号" />
        </el-form-item>
        <el-form-item label="主信贷人姓名" prop="reviewName">
          <el-input v-model="form.reviewName" placeholder="请输入主信贷人姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="reviewNumber">
          <el-input v-model="form.reviewNumber" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="评议类型" prop="reviewType">
          <el-input v-model="form.reviewType" placeholder="请输入评议类型" />
        </el-form-item>
        <el-form-item label="评议员" prop="reviewerId">
          <el-select v-model="form.reviewerId" placeholder="请选择评议员">
            <el-option
              v-for="item in senatorList"
              :key="item.reviewerId"
              :label="item.reviewerName"
              :value="item.reviewerId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="评议监督员" prop="reviewSupervisor">
          <el-input v-model="form.reviewSupervisor" placeholder="请输入评议监督员" />
        </el-form-item>
        <el-form-item label="评议时间" prop="reviewTime">
          <el-date-picker clearable
                          v-model="form.reviewTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择评议时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否了解信贷人情况" prop="reviewKnow1">
          <el-select v-model="form.reviewKnow1" placeholder="请选择是否了解信贷人情况">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否存在以下情况" prop="reviewKnow2">
          <el-select v-model="form.reviewKnow2" placeholder="请选择是否存在以下情况">
            <el-option
              v-for="dict in dict.type.bank_know2"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="农村房产情况" prop="reviewKnow3">
          <el-select v-model="form.reviewKnow3" placeholder="请选择农村房产情况">
            <el-option
              v-for="dict in dict.type.bank_know3"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="城区有无房产" prop="reviewKnow4">
          <el-select v-model="form.reviewKnow4" placeholder="请选择城区有无房产">
            <el-option
              v-for="dict in dict.type.bank_know4"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="有无车辆" prop="reviewKnow5">
          <el-select v-model="form.reviewKnow5" placeholder="请选择有无车辆">
            <el-option
              v-for="dict in dict.type.bank_know5"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年收入" prop="reviewKnow6">
          <el-select v-model="form.reviewKnow6" placeholder="请选择年收入">
            <el-option
              v-for="dict in dict.type.bank_know6"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工作类型" prop="reviewKnow7">
          <el-select v-model="form.reviewKnow7" placeholder="请选择工作类型">
            <el-option
              v-for="dict in dict.type.bank_know7"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="主营项目" prop="reviewKnow8">
          <el-input v-model="form.reviewKnow8" placeholder="请输入主营项目" />
        </el-form-item>

        <el-form-item label="备注" prop="reviewNotes">
          <el-input v-model="form.reviewNotes" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReview, getReview, delReview, addReview, updateReview } from "@/api/bank/review";
import {listVillage} from "@/api/bank/village";
import {listSenator} from "@/api/bank/senator";
import {getUserProfile} from "@/api/system/user";


export default {
  name: "Review",
  dicts: ['bank_know2', 'bank_know4', 'bank_know3', 'bank_know6', 'bank_know5', 'sys_yes_no', 'bank_know7'],
  data() {
    return {
      // 遮罩层
      loading: true,

      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 评议界面表格数据
      reviewList: [],
      // 评议员数据
      senatorList:[],
      // 行政村数据
      villageList:[],
      //当前用户信息
      user: {},
      // 是否设置了查询条件
      isQuerySet: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        reviewName: null,
        reviewVillageName:null,
        'bankSenator.reviewerName':null,
        reviewKnow1: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        reviewVillageName:[
          { required: true, message: "村名字不能为空", trigger: "blur" }
        ],
        reviewDomicileNumber: [
          { required: true, message: "评议户籍号不能为空", trigger: "blur" }
        ],
        reviewName: [
          { required: true, message: "主信贷人姓名不能为空", trigger: "blur" }
        ],
        reviewNumber: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        reviewType: [
          { required: true, message: "评议类型不能为空", trigger: "blur" }
        ],
        reviewerId: [
          { required: true, message: "评议员不能为空", trigger: "blur" }
        ],
        reviewSupervisor: [
          { required: true, message: "评议监督员不能为空", trigger: "blur" }
        ],
        reviewTime: [
          { required: true, message: "评议时间不能为空", trigger: "blur" }
        ],
        reviewKnow1: [
          { required: true, message: "是否了解信贷人情况不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getUser();
  },
  methods: {
    /** 查询当前用户信息，并筛选行政村和评议员和评议表 */
    getUser() {
      getUserProfile().then(response => {
        this.user = response.data;
        this.getVillageList();
      });
    },

    /** 查询行政村列表，并根据当前用户ID过滤 */
    getVillageList() {
      listVillage({pageSize:1000000}).then(response => {
        // 确认response.rows是一个数组且不为空
        if (Array.isArray(response.rows) && response.rows.length) {
          // 过滤出当前用户管理的行政村
          if (this.user.userId != 17 && this.user.userId != 1) {
            this.villageList = response.rows.filter(village => village.superVisorId == this.user.userId);
          } else {
            // 如果是特定ID，则不进行过滤，直接使用全部行政村数据
            this.villageList = response.rows;
          }
        }
        // 根据过滤后的行政村ID获取评议员信息
        this.getSenatorList();
      });
    },
    getSenatorList() {
      listSenator({pageSize:10000000}).then(response => {
        // 确认response.rows是一个数组且不为空
        if (Array.isArray(response.rows) && response.rows.length) {
          // 过滤出当前行政村下的评议员
          this.senatorList = response.rows.filter(senator =>
            this.villageList.some(village => village.villageId == senator.villageId));
        }
        // 根据过滤后的评议员id得到评议表
        this.getReviewList();
      });
    },
    /** 根据评议员筛选并获取评议信息列表 */
    getReviewList() {
      // 确保有评议员被选择
      if (this.isQuerySet) {
        this.loading = true;

        // 调用获取评议信息的API，传递评议员ID和分页参数
        listReview(this.queryParams).then(response => {
          this.reviewList = response.rows; // 设置当前页的评议信息
          this.total = response.total; // 设置总条数，确保从后端响应中获取
          this.loading = false;
        }).catch(error => {
          console.error("获取评议信息失败:", error);
          this.loading = false;
        });
      } else {
        // 如果没有选择评议员，清空列表并重置总条数
        this.reviewList = [];
        this.total = 0;
      }
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.isQuerySet = this.queryParams['bankSenator.reviewerName'] ||
        this.queryParams.reviewVillageName ||
        this.queryParams.reviewName ||
        this.queryParams.reviewKnow1;
      if (this.isQuerySet) {
        this.queryParams.pageNum = 1;
        this.getReviewList();
      }
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        reviewId: null,
        reviewerId: null,
        reviewVillageName:null,
        reviewDomicileNumber: null,
        reviewName: null,
        reviewNumber: null,
        reviewType: null,

        reviewSupervisor: null,
        reviewTime: null,
        reviewKnow1: null,
        reviewKnow2: null,
        reviewKnow3: null,
        reviewKnow4: null,
        reviewKnow5: null,
        reviewKnow6: null,
        reviewKnow7: null,
        reviewKnow8: null,
        reviewNotes: null
      };
      this.resetForm("form");
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.reviewId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加评议界面";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const reviewId = row.reviewId || this.ids
      getReview(reviewId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改评议界面";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.reviewId != null) {
            updateReview(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getReviewList();
            });
          } else {
            addReview(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getReviewList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const reviewIds = row.reviewId || this.ids;
      this.$modal.confirm('是否确认删除评议界面编号为"' + reviewIds + '"的数据项？').then(function() {
        return delReview(reviewIds);
      }).then(() => {
        this.getReviewList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bank/review/export', {
        ...this.queryParams
      }, `review_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
